package com.zym.init.aop;

import cn.dev33.satoken.stp.StpUtil;
import com.zym.init.annotation.RoleCheck;
import com.zym.init.exception.RoleException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * 权限校验 AOP
 */
@Aspect
@Component
public class RoleInterceptor {

    /**
     * 执行拦截
     *
     * @param joinPoint 切点
     * @param roleCheck 角色校验注解
     * @return Object
     */
    @Around("@annotation(roleCheck)")
    public Object doInterceptor(ProceedingJoinPoint joinPoint, RoleCheck roleCheck) throws Throwable {
        String role = roleCheck.role();
        if (!StpUtil.hasRole(role)) {
            throw new RoleException();
        }
        // 通过权限校验，放行
        return joinPoint.proceed();
    }
}
